import store from '../../vuex/store';
import Vue from "vue";

export default {
    payGoodMan: "ok man",
    payPam: {},
    PayParent:null,
    GetPayCode(cookieStr) {
      let self = this;
      //"userid:" + uid + ",goodsid:" + gid + ",types:" + types
      self.setCookie("payMsg", cookieStr);
      self.setCookieMin("buyState", "paycode", 0.5);

      window.location.href = store.state.domainUrl+"wxapi/Pay/example/oauth2.php?tourl="+store.state.path+"payback.html";
    },
    payHandle(payobj,payPar) {
      this.PayParent = payPar;
      let self = this;
      var codestate = self.getCookie("buyState");
      console.log("codestate = " + codestate);

      if (codestate == "waitpayToServer") {
        console.log("ttt = " + self.getCookie("payMsg")+"type="+payobj.dptype+"cli="+payobj.clientid+"sid="+payobj.salesmanid);
        self.setCookie("buyState", "payToServer");
        var codes = self.getCookie("paycode");
        console.log("codes = " + codes);

        var tourls = '';
        var types = payobj.types;
        var toobjs = {};
        if (types == "GoodsPayParam") {
          var uid = payobj.uid;
          var gid = payobj.gid;

          toobjs = {
            type: types,
            goodsid: gid,
            userid: uid,
            code: codes,
            num:payobj.num
          };
          tourls = store.state.domainUrl+'wxapi/Pay/example/pay.php';

        } else if (types == "DepositPayParam") {
          // {type:'DepositPayParam',dptype:1,clientid:1,salesmanid:2,code:depositPayData.codes};
          toobjs = {
            type: types,
            dptype: payobj.dptype,
            clientid: payobj.clientid,
            salesmanid: payobj.salesmanid,
            code: codes
          };
          tourls = store.state.domainUrl+'wxapi/Pay/example/pay.php';
        }

        Vue.prototype.$http
          .post(tourls, toobjs)
          .then(response => {
            let respdata = response.body;
            var atk = respdata;
            console.log("atk=" + atk);
            self.payPam = respdata; //JSON.parse(respdata);
            if (self.payPam.error != "ok") {
              alert(self.payPam.error);
              return;
            }


            self.callpay();


          })
          .catch(function(response) {
            //请求出错处理
            console.log(response);
          });
      }
    },
    editAddress(eobj) {
      let self = this;
      WeixinJSBridge.invoke("editAddress", eobj, function(res) {
        var value1 = res.proviceFirstStageName;
        var value2 = res.addressCitySecondStageName;
        var value3 = res.addressCountiesThirdStageName;
        var value4 = res.addressDetailInfo;
        var tel = res.telNumber;

        self.callpay();
      });
    },
    jsApiCall() {
      let self = this;
      WeixinJSBridge.invoke(
        "getBrandWCPayRequest",
        JSON.parse(self.payPam.jsapi),
        function(res) {
          WeixinJSBridge.log(res.err_msg);
          // alert(res.err_code + '..'+res.err_desc + '..'+res.err_msg);
          if (res.err_msg == "get_brand_wcpay_request:ok") {
            //to url
            // alert("tourl=");
            if(self.PayParent)
              self.PayParent.PaySuccess();
          }
        }
      );
    },
    callpay() {
      let self = this;
      if (typeof WeixinJSBridge == "undefined") {
        if (document.addEventListener) {
          document.addEventListener(
            "WeixinJSBridgeReady",
            self.jsApiCall,
            false
          );
        } else if (document.attachEvent) {
          document.attachEvent("WeixinJSBridgeReady", self.jsApiCall);
          document.attachEvent("onWeixinJSBridgeReady", self.jsApiCall);
        }
      } else {
        self.jsApiCall();
      }
    },
    getQueryString(name) {
      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
      var r = window.location.search.substr(1).match(reg);
      if (r != null) return unescape(r[2]);
      return null;
    },
    setCookieMin(name, value, times) {
      var exp = new Date();
      exp.setTime(exp.getTime() + times * 60 * 1000);
      document.cookie =
        name + "=" + escape(value) + ";expires=" + exp.toGMTString();
    },
    setCookie(name, value) {
      var Days = 30;
      var exp = new Date();
      exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
      document.cookie =
        name + "=" + escape(value) + ";expires=" + exp.toGMTString();
    },
    getCookie(name) {
      var arr,
        reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");

      if ((arr = document.cookie.match(reg))) return unescape(arr[2]);
      else return null;
    }
};
